﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows;
using WPFPractice.Windows;

namespace WPFPractice
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 服务提供者
        /// </summary>
        public static IServiceProvider ServiceProvider { get; private set; }

        public App()
        {
            IServiceCollection services = new ServiceCollection();
            services.AddSingleton<ITextService, TextService>();
            //services.AddSingleton<WinDITester>();
            services.AddTransient<WinDITester>();

            IServiceProvider serviceProvider = services.BuildServiceProvider();
            App.ServiceProvider = serviceProvider; //便于全局使用;
        }
    }

    /// <summary>
    /// 测试依赖注入的接口
    /// </summary>
    public interface ITextService
    {
        string GetText();
    }

    /// <summary>
    /// 测试依赖注入的实现类
    /// </summary>
    public class TextService : ITextService
    {
        public string GetText()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}
